import {
  saveZGCheckBusinessInfo,
  getCSBList,
  getCheckInfoBusinessById
} from '../../../api/check'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    autosize: {
      maxHeight: 200,
      minHeight: 80
    },
    zgjgPopupshow: false,
    content: '',
    lxData: {},
    checkmxlx: [], //问题类型
    checkxmzg: [],
    checkInfoDetail: [], //检查项目详细信息
    zgjgData: {}, //选择的整改结果对象
    zgms: '', //整改描述字段
    zgFileList: [], //整改图片数组集合
  },
  getSelect(type) {
    return getCSBList({
      type: type
    }).then(res => {
      if (type == 'checkmxlx') {
        this.setData({
          checkmxlx: res
        })
      } else {
        this.setData({
          checkxmzg: res
        })
      }
    })
  },
  // 图片预览
  onImageClick(e) {
    const urls = this.data.checkInfoDetail.fileList.map(item => {
      return item.url
    })
    wx.previewImage({
      urls: urls,
      current: e.mark.item.url, // 当前显示的图片地址
    })
  },
  // 获取检查详细信息
  getlist() {
    getCheckInfoBusinessById({
      id: this.data.ywid,
    }).then(res => {
      if (res.code == 1000) {
        const info = res.data[0];
        let zgjgData = [];
        if (info.zgjg) {
          zgjgData = this.data.checkxmzg.filter(item => {
            return item.bm == info.zgjg
          })
        }
        this.setData({
          checkInfoDetail: info,
          zgms: info.zgms || '',
          zgjgData: zgjgData[0] || {},
          zgFileList: info.zgfileList || []
        })
        this.initData()
      }
    })
  },
  initData() {
    let lxData = this.data.checkmxlx.filter(item => {
      return item.bm == this.data.checkInfoDetail.lx
    })[0]
    this.setData({
      lxData
    })

  },
  //整改结果下拉框
  zgjgPopup() {
    this.setData({
      zgjgPopupshow: true
    });
  },
  //整改结果确认
  zgjgConfirm(event) {
    this.setData({
      zgjgData: event.detail.value,
      zgjgPopupshow: false
    })
  },
  zgjgCancel() {
    this.setData({
      zgjgPopupshow: false
    })
  },
  //解释说明
  showdetailPopup(event) {
    let content = event.currentTarget.dataset.item.flfgname;
    this.setData({
      content: content,
      show: true
    });
  },
  ondetailClose() {
    this.setData({
      show: false
    });
  },
  //图片上传
  afterRead(event) {
    let that = this;
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://njaqs.top/securityinspection/fileUpload/uploadFile',
      filePath: file.url,
      name: 'file',
      success: (res) => {
        const data = JSON.parse(res.data)
        const zgfileList = that.data.zgFileList || [];
        zgfileList.push({
          url: data.fileUrl,
          name: data.filename,
          fileurl: data.url,
          type: 'image'
        })
        that.setData({
          zgFileList: zgfileList
        });
      },
    });
  },
  //删除图片
  delete(event) {
    const zgfileList = this.data.zgFileList
    zgfileList.splice(event.detail.index, 1)
    this.setData({
      zgFileList: zgfileList
    });
  },
  //整改描述
  zgPopup(event) {

  },
  close() {
    wx.navigateBack({
      delta: 1
    })
  },
  //提交
  submint() {
    if (!this.data.zgjgData.bm) {
      wx.showToast({
        title: '请选择整改结果',
        icon: 'error'
      })
      return
    }
    saveZGCheckBusinessInfo({
      id: this.data.checkInfoDetail.id,
      zgms: this.data.zgms,
      zgjg: this.data.zgjgData.bm,
      url1: this.data.zgFileList[0] ? this.data.zgFileList[0].fileurl : '',
      url2: this.data.zgFileList[1] ? this.data.zgFileList[1].fileurl : '',
      url3: this.data.zgFileList[2] ? this.data.zgFileList[2].fileurl : '',
      logintoken: wx.getStorageSync("userinfo").logintoken
    }).then(res => {
      if (res.code == 1000) {
        wx.showToast({
          title: '提交成功!',
          icon: 'success',
          duration: 1500, // 图标停留时间，默认1500ms
          success: function () {
            wx.navigateBack({
              delta: 1
            })
          }
        });
      }
    })
  },
  onChange(event) {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getSelect('checkxmzg')
    this.getSelect('checkmxlx').then(() => {
      this.getlist()
    })
    this.setData({
      ywid: options.ywid,
      qyname: options.qyname
    })
    this.setData({
      isCompay: wx.getStorageSync("userinfo").userlx2
    })
    //下拉框
    //调用列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})